package com.example.web.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.web.dto.query.AnalySeInput;
import com.example.web.entity.*;

import com.example.web.mapper.AppUserMapper;

import com.example.web.mapper.GoodMapper;
import com.example.web.mapper.OrderInfoMapper;

import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.*;

/**
 * 决策分析
 */
@RestController()
@RequestMapping("/Analyse")
public class AnalyseController {
    @Autowired()
    private AppUserMapper _AppUserMapper;

    @Autowired()
    private GoodMapper _GoodMapper;
    @Autowired()
    private OrderInfoMapper _OrderInfoMapper;

    /**
     * 个人信息分析
     */
    @RequestMapping(value = "/MyAnalyse", method = RequestMethod.POST)
    @SneakyThrows
    public Map<String, Object> MyAnalyse(@RequestBody AnalySeInput input) {


        List<OrderInfo> list = _OrderInfoMapper.selectList(null).stream().filter(x -> x.getOrderStatus() != Enums.OrderStatus.取消.index()).toList();
        //得到我买入的订单笔数
        long buycount = list.stream().filter(x -> x.getBuyUserId() == input.getUserId()).count();
        //得到我卖出的订单笔数
        long sellcount = list.stream().filter(x -> x.getSellerUserId() == input.getUserId()).count();


        List<Good> goods = _GoodMapper.selectList(null).stream().filter(x -> x.getCreatorId() == input.getUserId()).toList();
        //我的商品总数


        Map<String, Object> data = new HashMap<>();
        data.put("buycount", buycount);
        data.put("sellcount", sellcount);
        data.put("goodcount", goods.stream().count());

        return data;
    }

    /**
     * 订单状态占比
     */
    @RequestMapping(value = "/OrderStatusAnalyse", method = RequestMethod.POST)
    @SneakyThrows
    public List<Map<String, Object>> OrderStatusAnalyse() {
        List<Map<String, Object>> dataList = new ArrayList<>();
        List<OrderInfo> list = _OrderInfoMapper.selectList(null).stream().toList();

        List<String> status = list.stream().map(x ->  Enums.OrderStatus.GetEnum(x.getOrderStatus()).toString()).distinct().toList();
        for (String paytype : status) {
            Map<String, Object> data1 = new HashMap<>();
            data1.put("name",paytype);
            long count = list.stream().filter(x -> Enums.OrderStatus.GetEnum(x.getOrderStatus()).toString().equals(paytype)).count();
            data1.put("value",count);
            dataList.add(data1);

        }

        return dataList;
    }

    /**
     * 最近7天销量
     */
    @RequestMapping(value = "/Last7DaySalesAnalyse", method = RequestMethod.POST)
    @SneakyThrows
    public List<Map<String, Object>> Last7DaySalesAnalyse() {
        List<Map<String, Object>> dataList = new ArrayList<>();
        List<OrderInfo> list = _OrderInfoMapper.selectList(null).stream().filter(x -> x.getOrderStatus() != Enums.OrderStatus.取消.index()).toList();

        for (int i = 0; i < 7; i++) {
            int day=i;
            Map<String, Object> data1 = new HashMap<>();
            data1.put("name", LocalDate.now().minusDays(i).toString());
            long count = list.stream().filter(x -> x.getCreationTime().toLocalDate().equals(LocalDate.now().minusDays(day))).count();
            data1.put("value",count);
            dataList.add(data1);

        }

        return dataList;
    }
    /**
     * 最近30天销量
     */
    @RequestMapping(value = "/Last30DaySalesAnalyse", method = RequestMethod.POST)
    @SneakyThrows
    public List<Map<String, Object>> Last30DaySalesAnalyse() {
        List<Map<String, Object>> dataList = new ArrayList<>();
        List<OrderInfo> list = _OrderInfoMapper.selectList(null).stream().filter(x -> x.getOrderStatus() != Enums.OrderStatus.取消.index()).toList();

        for (int i = 0; i < 30; i++) {
            int day=i;
            Map<String, Object> data1 = new HashMap<>();
            data1.put("name", LocalDate.now().minusDays(i).toString());
            long count = list.stream().filter(x -> x.getCreationTime().toLocalDate().equals(LocalDate.now().minusDays(day))).count();
            data1.put("value",count);
            dataList.add(data1);

        }

        return dataList;
    }


}
